//
//  SecondViewModel.swift
//  RXSwiftDemo
//
//  Created by Apple on 2018/7/25.
//  Copyright © 2018年 LuckyStrike. All rights reserved.
//

import Foundation
import ReactiveSwift
import Result

final class SecondViewModel {
    
    // MARK: - 0.创建信号的方法
    func createSignalMehods() {
        // 1.通过信号发生器创建(冷信号)
        let producer = SignalProducer<String, NoError>.init { (observer, _) in
            print("新的订阅，启动操作")
            observer.send(value: "Hello")
            observer.send(value: "World")
        }
        
        let subscriber1 = Signal<String, NoError>.Observer.init { (event) in
            print("观察者1接收到值 \(event.value ?? "没收到")")
        }
        
//        let subscriber1 = Signal.Observer(value: { print("观察者1接收到值 \($0)") })
//        let subscriber2 = Signal.Observer(value: { print("观察者2接收到值 \($0)") })
        
        print("观察者1订阅信号发生器")
        producer.start(subscriber1)
        print("观察者2订阅信号发生器")
//        producer.start(subscriber2)
        //注意：发生器将再次启动工作
        
        // 2.通过管道创建（热信号）
        let (signalA, observerA) = Signal<String, NoError>.pipe()
        let (signalB, observerB) = Signal<String, NoError>.pipe()
        Signal.combineLatest(signalA, signalB).observeValues { (value) in
            print( "收到的值\(value.0) + \(value.1)")
        }
        observerA.send(value: "1")
        observerA.sendCompleted()
        observerB.send(value: "2")
        observerB.sendCompleted()
    }
    
}
